/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.tech15.core.action;

import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.CtgData;
import javacommon.util.LoginInfo;
import javacommon.util.Notes;
import javacommon.util.ResInfo;
import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.core.model.Group;
import com.tech15.core.model.GroupUser;
import com.tech15.core.service.GroupManager;
import com.tech15.core.service.ModuleManager;
import com.tech15.core.vo.query.GroupQuery;
import com.tech15.core.vo.query.GroupUserQuery;


/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class GroupAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String CHECKPERLIST_JSP= "/sys/group_permission_list.jsp";
	protected static final String LIST_JSP= "/sys/group_list.jsp";
	protected static final String GROUPUSERS_JSP= "/sys/group_user_list.jsp";
	protected static final String PRIGROUP_JSP= "/sys/group_select.jsp";
	protected static final String PRIGROUP_JSP1= "/sys/group1_select.jsp";
	protected static final String PRIGROUP_JSP2= "/sys/group2_select.jsp";
	protected static final String CREATE_JSP = "/sys/group_add.jsp";
	protected static final String EDIT_JSP = "/sys/group_edit.jsp";
	protected static final String SHOW_JSP = "/sys/group_show.jsp";
	//redirect paths,startWith: !
	
	private GroupManager groupManager;
	private ModuleManager moduleManager;
	private Group group;
	Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			group = new Group();
		} else {
			group = (Group)groupManager.getById(id);
		}
	}
	
	public void setGroupManager(GroupManager manager) {
		this.groupManager = manager;
	}
	public void setModuleManager(ModuleManager moduleManager) {
		this.moduleManager = moduleManager;
	}
	
	public Object getModel() {
		return group;
	}
	
	public void setPgid(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	/** 获取模块列表 设置组权限 */
	public String checkPer() {
		List<CtgData> list = moduleManager.findAuthModuleTree(id,"2",group.getAppid());
		getRequest().setAttribute("list", list);
		return CHECKPERLIST_JSP;
	}
	
	/** 执行搜索 */
	public String list() {
		GroupQuery query = newQuery(GroupQuery.class,DEFAULT_SORT_COLUMNS);
		query.setIsshfw(0);
		Page page = groupManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	public String usersInGroup() {
		GroupUserQuery query = newQuery(GroupUserQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = groupManager.searchGroupUsers(query);
		savePage(page,query);
		return GROUPUSERS_JSP;
	}
	
	public String findAll() {
		LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
		List<Group> list = groupManager.findAll(Notes.APPID);
		getRequest().setAttribute("pglist", list);
		return PRIGROUP_JSP1;
	}
	
	public String findAll2() {
		List<Group> list = groupManager.findAll();
		getRequest().setAttribute("pglist", list);
		return PRIGROUP_JSP2;
	}
	
	public String selectGroup(){
		List<Group> list = groupManager.selectGroup(0);
		getRequest().setAttribute("pglist", list);
		return PRIGROUP_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
		Map<String, Object> resInfo = null;
		try{
			group.setIsshfw(0);
			groupManager.save(group);
			resInfo = ResInfo.getResInfo("",false, false, 2, "操作成功","");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String edit() {
		List<GroupUser> list = groupManager.selectGroupUsersByPgid(id);
		getRequest().setAttribute("pglist",list);
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			groupManager.update(this.group);
			resInfo = ResInfo.getResInfo("",false, false, 2, "操作成功","");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try{
			groupManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, "操作成功","");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}
		outXmlJson(resInfo);
	}


}
